package cn.com.ddapp.ddm.fragment.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.ShareListAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.CircleBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.LoginActivity;
import cn.com.ddapp.ddm.ui.NearStoreActivity;
import cn.com.ddapp.ddm.ui.ToShareActivity;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/8/16 0016.
 */
public class FoundFragment extends BaseFragment {
    @BindView(R.id.share_refresh)
    PullToRefreshLayout share_refresh;
    @BindView(R.id.share_list)
    PullableListView share_list;
    @BindView(R.id.empty)
    EmptyLayout emptyLayout;
    @BindView(R.id.pingban)
    RelativeLayout pingban;
    private ShareListAdapter shareListAdapter;
    private List<CircleBean.CircleList> stringList;
    private int st;
    private String userId;
    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        if(TDevice.isTablet(BaseApplication.getIntstance())){
            pingban.setVisibility(View.GONE);
        }else{
            pingban.setVisibility(View.VISIBLE);
        }

        userId= SPUtils.getStringData(getActivity(),SPUtils.USERID);
        Log.i("yu","-userId-"+userId);
        requestData(true,0);
        share_refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                requestData(false,0);
                share_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                st=0;
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                st=st+10;
                requestLoadData(false,st);
            }
        });
    }



    private void requestData(final boolean isFirst,int st) {
        if(isFirst){
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        }
        final OkUtilCallback okUtilCallback=new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                System.out.println("HuaG=="+response);
                CircleBean bean=gson.fromJson(response,CircleBean.class);
                if(!bean.getResult().equals("OK")){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else if(bean.getResult().equals("OK") && bean.getCircleList().size()==0){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }else{
                    stringList=bean.getCircleList();
                    shareListAdapter=new ShareListAdapter(getActivity(),stringList);
                    share_list.setAdapter(shareListAdapter);

                }
            }
        };
        String params="userId=" +""+"&st="+st;
        OKUtilAPI.post(OKUtilAPI.circlelist, params, okUtilCallback);
    }

    private void requestLoadData(boolean b, final int s) {
        if(b){
            emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        }
        final OkUtilCallback okUtilCallback=new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                CircleBean bean=gson.fromJson(response,CircleBean.class);

                  List<CircleBean.CircleList>  sList=bean.getCircleList();
                    if(sList.size()>0 ){
                        shareListAdapter.addView(sList);
                        share_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                        st=s;
                    }else{
                        share_refresh.loadmoreFinish(PullToRefreshLayout.NODATA);
                    }


            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        String params="userId=" + ""+"&st="+st;
        OKUtilAPI.post(OKUtilAPI.circlelist, params, okUtilCallback);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.found_fragment;
    }

    @OnClick({R.id.right_share,R.id.nearshop,R.id.img_error_layout})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.right_share:
                if (SPUtils.getBooleanData(getActivity(), SPUtils.ISLOGIN)) {
                    if (!NetUtils.isConnected(getActivity())) {
                        Toast.makeText(getActivity(), "请检查你的网络", Toast.LENGTH_SHORT).show();
                    }else{
//                        ToShareActivity.startIntent(getActivity());
                        Intent intent=new Intent(getActivity(),ToShareActivity.class);
                        startActivityForResult(intent,258);
                    }
                }else {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(intent, AppConfig.MAIN_TAB_2);
                }
                AppConfig.TOMAIN="3";
                break;
            case R.id.nearshop:
                NearStoreActivity.startIntent(getActivity());
                AppConfig.TOMAIN="3";
                break;
            case R.id.img_error_layout:
                requestData(true,0);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 258:
                if(resultCode==555){
                    requestData(true,0);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        requestData(true,0);
    }
}
